﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Customer_Profile.Admin;
using System.IO;
using System.Net.Cache;

namespace Customer_Profile
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["usermodel"] != null)
            {
                string strUserID = "";
                string strUserName = "";
                string strEntityID = "";
                string strEntity = "";
                try
                {
                    strUserID = ((UserInfoEntity)Session["usermodel"]).UserLoginId;
                    strUserName = ((UserInfoEntity)Session["usermodel"]).UserName;
                    strEntityID = ((UserInfoEntity)Session["usermodel"]).UserEntityID;
                    strEntity = ((UserInfoEntity)Session["usermodel"]).UserOffice;
                }
                catch (Exception ex)
                {
                    KVB.Common.Log.WriteDefaultLog("LT-Session转化出错：", ex.ToString());
                }
                KVB.Common.Log.WriteDefaultLog("LT-UserInfo:", strUserID+"|"+strUserName+"|"+strEntity+"|"+strEntityID);
                Response.Write("已经有用户登录！一台机器不能同时登陆多个账号！");
                Response.End();
            }

            if (!Page.IsPostBack)
            {
                Session.Clear();
                Session.Abandon();
                Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
                Response.Cookies["ASP.NET_SessionId"].Expires = System.DateTime.Now.AddDays(-1);
            }
            this.Form.DefaultButton = "imgButton";
        }

        protected void imgButton_Click(object sender, ImageClickEventArgs e)
        {            
            if (String.Compare(Request.Cookies["CheckCode"].Value, VCode.Text, true) != 0)
            {                
                lblWarning.Text = "验证码错误!";
            }
            else
            {
                try
                {
                    string name = this.txtuserid.Value.Trim();
                    string pwdValue = this.txtpassword.Value.Trim();
                    //MD5加密
                    string pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwdValue, "MD5").ToLower();

                    UserInfoEntity model = new UserInfoEntity();
                    UserDao da = new UserDao();
                    model = da.getModel(name, pwd);
                    if (model != null)
                    {
                        Session["usermodel"] = model;
                        Response.Redirect("/main.aspx", false);
                    }
                    else
                    {
                        lblWarning.Text = "用户名或密码错误!";
                    }
                }
                catch (Exception ex)
                {
                    KVB.Common.Log.WriteDefaultLog("imgButton_Click", ex.ToString());
                }
            }
        }

        protected void ChangeCode_Click(object sender, EventArgs e)
        {

        }
    }
}